local hx__bianlian = fk.CreateSkill {
  name = "hx__bianlian",
  tags = { Skill.Switch },
}


hx__bianlian:addEffect(fk.AfterCardsMove, {
  name = "hx__bianlian",
  mute = true,
  switch_skill_name = "hx__bianlian",
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__bianlian.name) or player.dead then
      return false
    end
    if event == fk.AfterCardsMove then
      for _, move in ipairs(data) do
        if move.to and move.to == player and move.toArea == Player.Hand and move.skillName ~= hx__bianlian.name then
          return true
        end
        for _, info in ipairs(move.moveInfo) do
          if move.to and move.from == player and info.fromArea == Player.Hand and move.skillName ~= hx__bianlian.name then
            return true
          end
        end
      end
    end
    return false
  end,
  on_cost = function(self, event, target, player, data)
    if player:getSwitchSkillState(hx__bianlian.name, true) == fk.SwitchYang then
      event:setCostData(self, player)
      return true
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skillState = player:getSwitchSkillState(hx__bianlian.name, true)
    if skillState == fk.SwitchYang then
      player:broadcastSkillInvoke(hx__bianlian.name,1)
      room:notifySkillInvoked(player, hx__bianlian.name,"drawcard")
      player:drawCards(1, hx__bianlian.name)
      room:recover{
        who = player,
        num = 1,
        reason = hx__bianlian.name,
      }
    else
      player:broadcastSkillInvoke(hx__bianlian.name,2)
      room:notifySkillInvoked(player, hx__bianlian.name,"offensive")
      local targets = table.filter(room:getAlivePlayers(), function(p) return not p.dead end)
      local to = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#hx__bianlian-choose",
        skill_name = hx__bianlian.name,
        cancelable = false,
      } )
      local chosenPlayer = to[1]
      if #chosenPlayer:getCardIds("he") > 0 then
        room:askToDiscard(chosenPlayer, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          prompt = "#hx__bianlian-discard",
          skill_name = hx__bianlian.name,
          cancelable = false,
        })
      end
      room:loseHp(chosenPlayer, 1, hx__bianlian.name)
    end
  end,
})
hx__bianlian:addEffect(fk.HpChanged, {
  mute = true,
  can_trigger = function(self, event, target, player, data)
    if not player:hasSkill(hx__bianlian.name) or player.dead then
      return false
    end
    if event == fk.HpChanged then
      if data.skillName ~= hx__bianlian.name then
        return target == player
      end
    end
    return false
  end,
  on_cost = function(self, event, target, player, data)
    if player:getSwitchSkillState(hx__bianlian.name, true) == fk.SwitchYang then
      event:setCostData(self, player)
      return true
    end
    return true
  end,
  on_use = function(self, event, target, player, data)
    local room = player.room
    local skillState = player:getSwitchSkillState(hx__bianlian.name, true)
    if skillState == fk.SwitchYang then
      player:broadcastSkillInvoke(hx__bianlian.name,1)
      room:notifySkillInvoked(player, hx__bianlian.name,"drawcard")
      player:drawCards(1, hx__bianlian.name)
      room:recover{
        who = player,
        num = 1,
        reason = hx__bianlian.name,
      }
    else
      player:broadcastSkillInvoke(hx__bianlian.name,2)
      room:notifySkillInvoked(player, hx__bianlian.name,"offensive")
      local targets = table.filter(room:getAlivePlayers(), function(p) return not p.dead end)
      local to = room:askToChoosePlayers(player,{
        targets = targets,
        min_num = 1,
        max_num = 1,
        prompt = "#hx__bianlian-choose",
        skill_name = hx__bianlian.name,
        cancelable = false,
      } )
      local chosenPlayer = to[1]
      if #chosenPlayer:getCardIds("he") > 0 then
        room:askToDiscard(chosenPlayer, {
          min_num = 1,
          max_num = 1,
          include_equip = true,
          prompt = "#hx__bianlian-discard",
          skill_name = hx__bianlian.name,
          cancelable = false,
        })
      end
      room:loseHp(chosenPlayer, 1, hx__bianlian.name)
    end
  end,
})

return hx__bianlian